import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('./views/Login.vue'),
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('./views/Home.vue'),
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('./views/Register.vue'),
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const username = sessionStorage.getItem('username');

    if (to.path !== '/login' && !username) {
        next({ path: '/login' });
    } else {
        next();
    }
});

export default router;
